# Author: williamjcm <w.jcm59@gmail.com>
# Contributor: mosra <mosra@centrum.cz>
# Based on the magnum-plugins Arch Linux PKGBUILD maintained by xyproto and the MSYS2/MinGW PKGBUILD templates

_realname=magnum-plugins
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2020.06
_basis_pkgver=2f43afcc97d0a5dafdb73b4e24e123cf9687a418
_meshopt_pkgver=97c52415c6d29f297a76482ddde22f739292446d
pkgrel=1
pkgdesc='Plugins for the Magnum C++11/C++14 graphics engine'
arch=('any')
url='https://magnum.graphics/'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-magnum>=$pkgver"
         "${MINGW_PACKAGE_PREFIX}-assimp"
         "${MINGW_PACKAGE_PREFIX}-devil"
         "${MINGW_PACKAGE_PREFIX}-faad2"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-ninja" 'unzip')
source=("${_realname}-${pkgver}.zip::https://github.com/mosra/magnum-plugins/archive/v${pkgver}.zip"
        # A commit that's before the UASTC support (which is not implemented
        # yet, because latest versions crash even on trivial tests)
        "https://github.com/BinomialLLC/basis_universal/archive/2f43afcc97d0a5dafdb73b4e24e123cf9687a418.tar.gz"
        "https://github.com/zeux/meshoptimizer/archive/97c52415c6d29f297a76482ddde22f739292446d.tar.gz")
noextract=("${_realname}-${pkgver}.zip")
sha256sums=('8e1e98674ef3f0276f68abbdfecef1d72ae95b5a812b78c1ff45dba10fc72e3f'
            'f983ab5bfc5c32474914a87d9ca572c7fb970bf96f7bc68385c08772f633639f'
            '57bd3ff2ae1c20a2319979013e36a4472372af20580d323a1ce15f0385d5ed75')

prepare() {
  cd ${srcdir}
  rm -rf ${_realname}-${pkgver}
  unzip ../${_realname}-${pkgver}.zip
}

build() {
    mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

    # Bundle Basis Universal
    ln -sf $srcdir/basis_universal-${_basis_pkgver}/ "$srcdir/${_realname}-${pkgver}/src/external/basis-universal"

    # Bundle meshoptimizer
    ln -sf $srcdir/meshoptimizer-${_meshopt_pkgver}/ "$srcdir/${_realname}-${pkgver}/src/external/meshoptimizer"

    declare -a extra_config
    if check_option "debug" "n"; then
        extra_config+=("-DCMAKE_BUILD_TYPE=Release")
    else
        extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
    fi

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
        ${MINGW_PREFIX}/bin/cmake \
            -G'Ninja' \
            -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
            -DWITH_ASSIMPIMPORTER=ON \
            -DWITH_BASISIMPORTER=ON \
            -DWITH_DDSIMPORTER=ON \
            -DWITH_DEVILIMAGEIMPORTER=ON \
            -DWITH_DRFLACAUDIOIMPORTER=ON \
            -DWITH_DRMP3AUDIOIMPORTER=ON \
            -DWITH_DRWAVAUDIOIMPORTER=ON \
            -DWITH_FREETYPEFONT=ON \
            -DWITH_FAAD2AUDIOIMPORTER=ON \
            -DWITH_HARFBUZZFONT=ON \
            -DWITH_ICOIMPORTER=ON \
            -DWITH_JPEGIMAGECONVERTER=ON \
            -DWITH_JPEGIMPORTER=ON \
            -DWITH_MINIEXRIMAGECONVERTER=ON \
            -DWITH_MESHOPTIMIZERSCENECONVERTER=ON \
            -DWITH_PNGIMAGECONVERTER=ON \
            -DWITH_PNGIMPORTER=ON \
            -DWITH_OPENGEXIMPORTER=ON \
            -DWITH_PRIMITIVEIMPORTER=ON \
            -DWITH_STANFORDIMPORTER=ON \
            -DWITH_STANFORDSCENECONVERTER=ON \
            -DWITH_STBIMAGECONVERTER=ON \
            -DWITH_STBIMAGEIMPORTER=ON \
            -DWITH_STBTRUETYPEFONT=ON \
            -DWITH_STBVORBISAUDIOIMPORTER=ON \
            -DWITH_TINYGLTFIMPORTER=ON \
            "${extra_config[@]}" \
            ../${_realname}-${pkgver}
    ninja
}

package() {
    cd "${srcdir}"/build-${CARCH}
    DESTDIR="${pkgdir}" ninja install

    install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" \
        "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
